
import xlwt
import xlrd
import datetime
import json
import io


# 导出表格
def exportExcelForSendItem(_itemOrders):
    workbook = xlwt.Workbook(encoding = 'utf-8')
    worksheet = workbook.add_sheet('批量发货模版')

    style = xlwt.XFStyle()
    #设置样式
    alignment = xlwt.Alignment()
    # 水平位置：0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐)
    alignment.horz = 0x01
    # 垂直方向：0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐)
    alignment.vert = 0x01
    style.alignment = alignment
    #设置字体
    font = xlwt.Font()
    # font.bold = True
    style.font = font

    # worksheet.col(0).width = 4000
    # worksheet.col(1).width = 3000
    # worksheet.col(2).width = 4000
    # worksheet.col(3).width = 4000

    row_first = ['订单号', '订单状态', '商品名称', '快递公司', '快递单号']
    # 设置第一行
    for col_index, title in enumerate(row_first):
        style1 = xlwt.XFStyle()
        font1 = xlwt.Font()
        font1.bold = True
        style1.font = font1
        worksheet.write(0, col_index, title, style1)
    row_index = 1
    for row_i, record in enumerate(_itemOrders):
        last_col_index = 0
        for col_index, title in enumerate(row_first):
            if title == '订单号':
                worksheet.write(row_index, col_index, record.orderId, style)
                worksheet.col(col_index).width = 6000
            if title == '购买用户ID':
                worksheet.write(row_index, col_index, record.user.code if record.user else '', style)
                worksheet.col(col_index).width = 2000
            if title == '购买用户手机号':
                worksheet.write(row_index, col_index, record.user.phone if record.user else '', style)
                worksheet.col(col_index).width = 3000
            if title == '订单状态':
                worksheet.write(row_index, col_index, record.get_orderStatus_display() or '未知', style)
                worksheet.col(col_index).width = 2000
            if title == '提货方式':
                worksheet.write(row_index, col_index, record.get_sendType_display() or '未知', style)
                worksheet.col(col_index).width = 2000
            addrinfo = {
                'contactName': '',
                'contactPhone': '',
                'addrInfo': ''
            }
            if record.addrinfo:
                addrinfo = json.loads(record.addrinfo)
            if title == '收货人':
                worksheet.write(row_index, col_index, addrinfo.get('contactName'), style)
                worksheet.col(col_index).width = 2000
            if title == '收货人手机号':
                worksheet.write(row_index, col_index, addrinfo.get('contactPhone'), style)
                worksheet.col(col_index).width = 4000
            if title == '送货地址':
                worksheet.write(row_index, col_index, addrinfo.get('addrInfo'), style)
                worksheet.col(col_index).width = 12000
            if title == '买家留言':
                worksheet.write(row_index, col_index, record.remark, style)
                worksheet.col(col_index).width = 4000
            if title == '支付方式':
                worksheet.write(row_index, col_index, record.get_payway_display() or '未知', style)
                worksheet.col(col_index).width = 2000
            if title == '下单时间':
                payTime = str(record.payTime)
                if payTime and len(payTime)>=19:
                    payTime = payTime[0:10] + ' ' + payTime[11:19]
                else:
                    payTime = ''
                worksheet.write(row_index, col_index, payTime, style)
                worksheet.col(col_index).width = 5000
            if title == '订单金额':
                worksheet.write(row_index, col_index, record.sellingPrice, style)
                worksheet.col(col_index).width = 2000
            if title == '买家实付金额':
                worksheet.write(row_index, col_index, record.realPay, style)
                worksheet.col(col_index).width = 2000
            last_col_index = col_index
            if title == '商品名称':
                if record.content and json.loads(record.content).get('orderItemList') and len(json.loads(record.content).get('orderItemList'))>0:
                    orderItemList = remove_duplicates_by_key(json.loads(record.content).get('orderItemList'), 'itemName')
                    # orderItemList = json.loads(record.content).get('orderItemList')
                    for item in orderItemList:

                        worksheet.write(row_index, last_col_index, item.get('itemName') or '', style)
                        worksheet.col(last_col_index).width = 16000
                        row_index = row_index+1
                else:
                    row_index = row_index+1
            if title == '快递公司':
                worksheet.write(row_index, col_index, '', style)
                worksheet.col(col_index).width = 4000
                last_col_index = col_index
            if title == '快递单号':
                worksheet.write(row_index, col_index, '', style)
                worksheet.col(col_index).width = 6000
                last_col_index = col_index
        
            
    output = io.BytesIO()
    workbook.save(output)
    output.seek(0)
    return output

def remove_duplicates_by_key(items, key):
    seen = set()
    unique_items = []
    for item in items:
        item_key = item.get(key)
        if item_key not in seen:
            seen.add(item_key)
            unique_items.append(item)
    return unique_items